<?php
/*
 * 账户相关
 */
namespace app\home\controller;

use app\common\controller\IndexBase;
use think\facade\Session;
use think\facade\Db;
use app\common\model\System;
class Pay extends IndexBase
{
    protected function initialize()
    {
        parent::initialize();
    }

    //支付页面
    public function index()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        return $this->fetch('',['info'=>Db::table('user')->find($uid)]);
    }
    //支付码
    public function paycode()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        return $this->fetch('',['info'=>Db::table('user')->find($uid)]);
    }

    //零钱
    public function wallet()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        return $this->fetch('',['info'=>Db::table('user')->find($uid)]);
    }
    //充值
    public function recharge()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        $info = Db::table('user')->find($uid);
        if ($this->request->isPost())
        {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0')
            {
                try{
                    $this->validate($param, 'recharge');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $info['pay_pwd'] !== morse_code($param['pwd'],$info['salt']) && $this->error('支付密码错误！');
            $data = [
                'user_id' => $info['id'],
                'user_name' => $info['name'],
                'recharge_type' => $param['recharge_type'],
                'money' => abs($param['num']),
                'name' => $param['name'],
                'img' => $param['img'],
                'create_time' => time()
            ];
            $save = Db::table('recharge')->insert($data);
            !$save && $this->error('充值失败！',url('@user'));
            $this->success('申请成功,请等待审核！');

        }
        return $this->fetch('',['info'=>$info]);
    }

    //充值记录
    public function recharge_log()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        return $this->fetch('',['data'=>Db::table('recharge')->where(['user_id'=>$uid])->paginate()]);
    }
    //理财通
    public function deposit()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        return $this->fetch('',['info'=>Db::table('deposit')->where('user_id',$uid)->value('money')]);
    }
    //转入理财通
    public function deposit_in()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        if ($this->request->isPost())
        {
            $param = $this->request->param();
            //充值操作

            $this->success('充值成功');
        }
        return $this->fetch('',['info'=>Db::table('user')->find($uid)]);
    }
    //转出理财通
    public function deposit_out()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        return $this->fetch('',['info'=>Db::table('deposit')->where('user_id',$uid)->value('money')]);
    }

    //账目明细
    public function account()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        $data = Db::table('account')->where(['user_id'=>$uid])->order('create_time','DESC')->select();
        return $this->fetch('',['data'=>$data]);
    }

    //我的提现
    public function withdraw()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        $data = Db::table('user')->find($uid);
        if ($this->request->isPost())
        {
            $param = $this->request->param();
            //验证规则
            $verify = input('_verify', true);
            if($verify!='0')
            {
                try{
                    $this->validate($param, 'withdraw');
                }catch (\Exception $e){
                    $this->error($e->getMessage());
                }
            }
            $param['code'] = abs($param['code']);//绝对值
            $data['pay_pwd'] !== morse_code($param['pwd'],$data['salt']) && $this->error('支付密码错误！');
            $param['code'] > $data['money'] && $this->error('可用余额不足！');
            $data =[
                'uid'           => $uid,
                'money'         => '-'.$param['code'],
                'change_desc'   => '自助申请提现',
                'change_type'   => 4
            ];
            $save = insert_account_log($data);
            !$save && $this->error($this->errorMsg);
            insert_user_log($uid,'申请提现【￥'.$param['code'].'】元');
            $this->success('提取成功');
        }
        $desc = Db::table('contract')->where(['contract_type'=>2])->find();
        return $this->fetch('',['data'=>$data,'desc'=>$desc]);
    }
    //提现记录
    public function withdraw_log()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        $data = Db::table('withdraw')->where(['user_id'=>$uid])->order('create_time','DESC')->paginate();
        return $this->fetch('',['data'=>$data]);
    }

    //会员转账
    public function turn()
    {
        $uid = $this->hashids->decode(UserId());
        !$uid && $this->redirect(url('@user')); // 登录直接跳转
        $info = Db::table('user')->find($uid);
        if ($this->request->isPost())
        {
            $param = $this->request->param();
            $param['code'] = abs($param['code']);//绝对值
            $ti_code = intval($param['code']);
            // $ti_code <= 0 && $this->error('单次转账金额必须为100的整数倍！');
            $info['pay_pwd'] !== morse_code($param['pwd'],$info['salt']) && $this->error('支付密码错误！');
            $ti_code>$info['money'] && $this->error('您的余额不足!');
            $js_info = DB::table('user')->where('name',trim($param['name']))->find();
            !$js_info && $this->error('未查询到接收用户，请查证后再试');
            $js_info['id'] ==$uid && $this->error('不能给自己转账哟！');
            //执行扣除本人积分操作
            $data =[
                'uid'           => $uid,
                'money'         => '-'.$ti_code,
                'change_desc'   => '自助转账给'.$js_info['name'].$ti_code.'元',
                'change_type'   => 2
            ];
            insert_account_log($data);
            //执行插入接收店铺积分操作
            $ins_data =[
                'uid'           => $js_info['id'],
                'money'         => $ti_code,
                'change_desc'   => '接收来自'.$info['name'].'的转账',
                'change_type'   => 2
            ];
            insert_account_log($ins_data);
            return $this->success('转账成功');
        }
        return $this->fetch('',['info'=>$info]);
    }



}